package com.forum.common.event;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 评论事件
 */
@Getter
@Setter
@NoArgsConstructor
public class CommentEvent extends MessageEvent {
    
    /**
     * 评论内容
     */
    private String commentContent;
    
    /**
     * 帖子标题
     */
    private String postTitle;
    
    /**
     * 父评论ID（如果是回复评论）
     */
    private Long parentCommentId;
    
    /**
     * 父评论内容（如果是回复评论）
     */
    private String parentCommentContent;
    
    /**
     * 构造一个帖子评论事件
     * 
     * @param senderId 评论者ID
     * @param receiverId 帖子作者ID
     * @param postId 帖子ID
     * @param postTitle 帖子标题
     * @param commentId 评论ID
     * @param commentContent 评论内容
     * @return 帖子评论事件
     */
    public static CommentEvent postComment(Long senderId, Long receiverId, Long postId, String postTitle, 
                                          Long commentId, String commentContent) {
        CommentEvent event = new CommentEvent();
        event.setType(EventType.COMMENT_CREATE);
        event.setSenderId(senderId);
        event.setReceiverId(receiverId);
        event.setResourceId(commentId);
        event.setCommentContent(commentContent);
        event.setPostTitle(postTitle);
        event.setContent("有人评论了您的帖子《" + postTitle + "》");
        event.setResourceType("COMMENT");
        event.setLink("/post/detail/" + postId + "#comment-" + commentId);
        return event;
    }
    
    /**
     * 构造一个评论回复事件
     * 
     * @param senderId 回复者ID
     * @param receiverId 原评论作者ID
     * @param postId 帖子ID
     * @param postTitle 帖子标题
     * @param commentId 回复评论ID
     * @param commentContent 回复内容
     * @param parentCommentId 被回复的评论ID
     * @param parentCommentContent 被回复的评论内容
     * @return 评论回复事件
     */
    public static CommentEvent commentReply(Long senderId, Long receiverId, Long postId, String postTitle,
                                           Long commentId, String commentContent, 
                                           Long parentCommentId, String parentCommentContent) {
        CommentEvent event = new CommentEvent();
        event.setType(EventType.COMMENT_REPLY);
        event.setSenderId(senderId);
        event.setReceiverId(receiverId);
        event.setResourceId(commentId);
        event.setCommentContent(commentContent);
        event.setPostTitle(postTitle);
        event.setParentCommentId(parentCommentId);
        event.setParentCommentContent(truncateContent(parentCommentContent, 20));
        event.setContent("有人回复了您在《" + postTitle + "》中的评论");
        event.setResourceType("COMMENT");
        event.setLink("/post/detail/" + postId + "#comment-" + commentId);
        return event;
    }
    
    /**
     * 截断内容到指定长度，并添加省略号
     */
    private static String truncateContent(String content, int maxLength) {
        if (content == null || content.length() <= maxLength) {
            return content;
        }
        return content.substring(0, maxLength) + "...";
    }
} 